/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.compomics.specificpeakfinder;

import java.io.File;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 *
 * @author Jonas
 */
public class ExecuteProgram {

    private static final org.apache.log4j.Logger LOGGER = org.apache.log4j.Logger.getLogger(ExecuteProgram.class);
    private Map<Integer, String[]> projects;

    public ExecuteProgram() {

        projects = new HashMap<Integer, String[]>();
        String[] runs_1 = {"19260", "19310", "19292", "18148b", "19317"};
        projects.put(2093, runs_1);
    }

    public static void main(String[] args) {
        ExecuteProgram executeProgram = new ExecuteProgram();
        executeProgram.doMain();
    }

    public void doMain() {
        
        MzmlReader mzmlReader = new MzmlReader();
        OutputWriter outputWriter = new OutputWriter();

        for (int projectID : projects.keySet()) {
            String[] runs = projects.get(projectID);
            for (int i = 0; i < runs.length; i++) {
                System.out.println("");                
                LOGGER.info("Processing file " + (i + 1));
                
                // Search the right FeatureXMLFile
                File parentDirectory = new File("C:\\Test\\An_Peakfinding\\Lumi\\mzmlFiles\\" + projectID);                                                      
                File mzmlFile = findFileByRunNumber(parentDirectory, runs[i]);
                List matchingSpectra = mzmlReader.iterateAllSpectra(mzmlFile);                               
                              
                //Write the updated MS2Identifications to file
                File spectraOutput = new File("C:\\Test\\An_Peakfinding\\Lumi\\" + projectID + "\\" + "\\foundPeptides\\" + runs[i] + ".csv");
                outputWriter.writeSpectrumVariablesToFile(matchingSpectra, spectraOutput, projectID);
            }
        }
    }

    // Check for the right mzmlFile by runNumber in the parentDirectory
    public File findFileByRunNumber(File parentDirectory, String runNumber) {
        File foundFile = null;

        if (parentDirectory.isDirectory()) {

            File[] files = parentDirectory.listFiles();
            for (File file : files) {
                if (file.getName().contains(runNumber)) {
                    foundFile = file;
                    break;
                }
            }
        }
        return foundFile;
    }
}
